//
//  HealthBankViewController.m
//  EFHealth
//
//  Created by nf on 16/10/5.
//  Copyright © 2016年 ef. All rights reserved.
//

#import "StudyViewController.h"
#import "InformationController.h"
#import "EFAdView.h"
#import "HYSegmentedControl.h"
#import "EFStudyBannerModel.h"
#import "EFStudyChannelModel.h"
#import "StudyChildViewController.h"

@interface StudyViewController ()<HYSegmentedControlDelegate,UIScrollViewDelegate,BaseModelProtocol>
@property (nonatomic,strong)UIView *bannerBgView ;

@property (nonatomic,strong)HYSegmentedControl *segment ;

@property (nonatomic,strong)UIScrollView *bgScrollView ;
@property (nonatomic,strong)EFStudyBannerModel *bannerModel ;
@property (nonatomic,strong)EFStudyChannelModel *channelModel ;
@property (nonatomic,strong)NSArray *channelArray;

@end

@implementation StudyViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)initView
{
    [super initView];
    [self.view addSubview:self.statusOrangeView];
    [self.view addSubview:self.bannerBgView];
    [self.bannerModel getStudyBannerData];
    [self.channelModel getStudyChannelData];
    
    //    [self.view addSubview:self.segment];
    //    [self.view addSubview:self.bgScrollView];
        
        
        //默认选中第一个
//        [self hySegmentedControlSelectAtIndex:0];
        
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
    if([baseModel isKindOfClass:[EFStudyBannerModel class]]){
        StudyBannerResponse *resp = (StudyBannerResponse *)response;
        NSArray <StudyBannerData> *array = resp.resObject;
        [self reloadBannerViewWith:array];
    }else if([baseModel isKindOfClass:[EFStudyChannelModel class]]){
        StudyChannelResponse *resp = (StudyChannelResponse *)response;
        NSArray <StudyChannelData> *array = resp.resObject;
        _channelArray = [NSArray arrayWithArray:array];
        NSMutableArray *titleArray = [NSMutableArray array];
        for (StudyChannelData *data in array) {
            [titleArray addObject:data.columnName];
        }
        
        self.segment = [[HYSegmentedControl alloc]initWithOriginY:self.bannerBgView.bottom+10 Titles:titleArray delegate:self lineHeight:2.0f titleColor:kTitleColor_black font:kFontMidden];
        [self.view addSubview:self.segment];
        
        [self.view addSubview:self.bgScrollView];
        
        for (int i = 0 ; i < _channelArray.count; i++) {
            StudyChildViewController *childVC = [[StudyChildViewController alloc]init];
            childVC.view.frame = CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH,self.bgScrollView.height);
            StudyChannelData *data = self.channelArray[i];
            childVC.pId = data.eid;
            [self addChildViewController:childVC ];
            [self.bgScrollView addSubview:childVC.view];
            self.bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*_channelArray.count, 0);
        }
            
        //默认选中第一个
        [self hySegmentedControlSelectAtIndex:0];

    }
}

- (void)reloadBannerViewWith:(NSArray *)response
{
    NSMutableArray *urlArray = [NSMutableArray array];
    for(int i = 0; i < response.count; i++){
        StudyBannerData *data = response[i];
        [urlArray addObject:data.indexPic];
    }
    
    [self.bannerBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    EFAdView *adView = [EFAdView adScrollViewWithFrame:CGRectMake(10, 10, self.bannerBgView.width-20, self.bannerBgView.height-20) modelArray:urlArray imagePropertyName:@"imgurl" titlePropertyName:@"name"];
    __weak typeof(self) weakSelf = self;
    adView.callBack = ^(NSInteger index,id model){
        StudyBannerData *data = response[index];
        InformationController *infoVC = [[InformationController alloc]init];
        infoVC.secondMenu = data.eid;
        [weakSelf.navigationController pushViewController:infoVC animated:YES];
    };
    [self.bannerBgView addSubview:adView];
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self.segment changeSegmentedControlWithIndex:scrollView.contentOffset.x/SCREEN_WIDTH];

}

- (void)hySegmentedControlSelectAtIndex:(NSInteger)index
{
    [self.bgScrollView setContentOffset:CGPointMake(SCREEN_WIDTH*(index), 0) animated:YES];
    
//    if(_pieceVC) return;
//    _pieceVC = [[PiecesRecordViewController alloc]init];
//    _pieceVC.view.frame = CGRectMake(SCREEN_WIDTH*index, 0, SCREEN_WIDTH,self.bgScrollView.height);
//    StudyChannelData *data = self.channelArray[index];
//    _pieceVC.pId = data.eid;
//    [self addChildViewController:_pieceVC ];
//    [self.bgScrollView addSubview:_pieceVC.view];
//    
//    switch (index) {
//        case 0:{
////            if (_diseaseVC)return ;
////            _diseaseVC = [[CureDiseaseViewController alloc]init];
////            _diseaseVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH,self.bgScrollView.height);
////            [self addChildViewController:_diseaseVC ];
////            [self.bgScrollView addSubview:_diseaseVC.view];
////            self.view.backgroundColor = [UIColor yellowColor];
//
//
//        }break;
//        case 1:{
////            if (_publicVC)return ;
////            _publicVC = [[PublicHealthViewController alloc]init];
////            _publicVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.bgScrollView.height);
////            [self addChildViewController:_publicVC ];
////            [_bgScrollView addSubview:_publicVC.view];
////            self.view.backgroundColor = [UIColor redColor];
//
//
//            
//        }break;
//    }
    
}

//- (HYSegmentedControl *)segment
//{
//    if (nil == _segment) {
//        _segment = [[HYSegmentedControl alloc]initWithOriginY:self.bannerBgView.bottom+10 Titles:@[@"医 院",@"公共卫生"] delegate:self lineHeight:2.0f titleColor:kTitleColor_black font:kFontMidden];
//    }
//    return _segment ;
//}
- (UIScrollView *)bgScrollView
{
    if (nil == _bgScrollView) {
        
        _bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.segment.bottom +1.0f, SCREEN_WIDTH, SCREEN_HEIGHT-KTarBarFullHeight-self.segment.bottom)];
        _bgScrollView.pagingEnabled = YES ;
        _bgScrollView.bounces = NO ;
//        _bgScrollView.scrollEnabled = NO ;
        _bgScrollView.delegate = self ;
    }
    return _bgScrollView ;
}

- (UIView *)bannerBgView
{
    if (nil == _bannerBgView) {
        _bannerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, self.statusOrangeView.height, SCREEN_WIDTH, 180)];
        _bannerBgView.backgroundColor = [UIColor whiteColor];
        _bannerBgView.userInteractionEnabled = YES ;
    }
    return _bannerBgView ;
}

- (EFStudyBannerModel *)bannerModel{

    if (nil == _bannerModel) {
        _bannerModel = [[EFStudyBannerModel alloc]initWithDelegate:self cache:NO];
    }
    return _bannerModel;
}

- (EFStudyChannelModel *)channelModel{

    if (nil == _channelModel) {
        _channelModel = [[EFStudyChannelModel alloc]initWithDelegate:self cache:NO];
    }
    return _channelModel;
}

@end
